﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace IllustrateCsharp7_ch21_netframework
{
    delegate long MyDel(int first, int second); //声明委托类型

    class Polling
    {
        static long Sum(int x, int y)           //声明异步方法
        {
            Console.WriteLine("             Inside Sum");
            Thread.Sleep(100);
            return x + y;
        }

        static void _Main()
        {
            MyDel del = new MyDel(Sum);

            IAsyncResult iar = del.BeginInvoke(3, 5, null, null); //发起异步方法
            Console.WriteLine("After BeginInvoke");

            while (!iar.IsCompleted)
            {
                Console.WriteLine("Not Done");
                //继续处理
                for (long i = 0; i < 1000000; i++) ;
            }
            Console.WriteLine("Done");

            long result = del.EndInvoke(iar);//调用EndInvoke来获取接口并进行清理
            Console.WriteLine("Result:{0}",result);
            Console.Read();
        }
    }
}
